package com.tools.web.email;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 发送邮箱操作时
 * 所需配置项的
 * 包装实体
 * */
@Note("发送邮箱操作时发送邮箱操作时包装实体")
public class MailSource {

    @Note("第三方邮箱厂商中，用于发送邮件的服务器主机名称")
    private String sendServerHost;

    @Note("第三方邮箱厂商中，用于接收邮件的服务器主机名称")
    private String receiveServerHost;

    @Note("第三方邮箱厂商提供的授权码")
    private String authCode;

    @Note("作为服务方，我们给别人发邮件的、自己的邮箱地址")
    private String sendMailer;

    @Note("作为接收方，我们用于接收别人发的邮件的邮箱地址")
    private String receiveMailer;

    @Note("邮箱协议，用于发送邮件，默认是 smtp")
    private MailProtocolEnum sendProtocol = MailProtocolEnum.SMTP;

    @Note("邮箱协议，用于接收邮件，默认是 imap")
    private MailProtocolEnum receiveProtocol = MailProtocolEnum.IMAP;

    @Note("发送邮件时我们作为发送者起的别名")
    private String sendAlias;

    @Note("作为接收方接收别人发过来的邮件时，我们用于展示的别名")
    private String receiveAlias;

    @Note("是否显示邮件发送接收的详细信息，默认为 false。也可以通过 Session.setDebug(boolean) 设置")
    private boolean enableDebug = false;


    public MailSource() {}

    public static MailSource of() {
        return new MailSource();
    }

    /* **********************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************
     * */

    public String getSendServerHost() {
        return sendServerHost;
    }

    public void setSendServerHost(String sendServerHost) {
        this.sendServerHost = sendServerHost;
    }

    public String getReceiveServerHost() {
        return receiveServerHost;
    }

    public void setReceiveServerHost(String receiveServerHost) {
        this.receiveServerHost = receiveServerHost;
    }

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

    public String getSendMailer() {
        return sendMailer;
    }

    public void setSendMailer(String sendMailer) {
        this.sendMailer = sendMailer;
    }

    public String getReceiveMailer() {
        return receiveMailer;
    }

    public void setReceiveMailer(String receiveMailer) {
        this.receiveMailer = receiveMailer;
    }

    public MailProtocolEnum getSendProtocol() {
        return sendProtocol;
    }

    public void setSendProtocol(MailProtocolEnum sendProtocol) {
        if(sendProtocol == null) throw new NullPointerException("发送邮件的邮件协议不能为 null");
        this.sendProtocol = sendProtocol;
    }

    public MailProtocolEnum getReceiveProtocol() {
        return receiveProtocol;
    }

    public void setReceiveProtocol(MailProtocolEnum receiveProtocol) {
        if(receiveProtocol == null) throw new NullPointerException("接收邮件的邮件协议不能为 null");
        this.receiveProtocol = receiveProtocol;
    }

    public String getSendAlias() {
        return sendAlias;
    }

    public void setSendAlias(String sendAlias) {
        this.sendAlias = sendAlias;
    }

    public String getReceiveAlias() {
        return receiveAlias;
    }

    public void setReceiveAlias(String receiveAlias) {
        this.receiveAlias = receiveAlias;
    }

    public boolean isEnableDebug() {
        return enableDebug;
    }

    public void setEnableDebug(boolean enableDebug) {
        this.enableDebug = enableDebug;
    }

    @Override
    public String toString() {
        return "MailSource{" +
                "sendServerHost='" + sendServerHost + '\'' +
                ", receiveServerHost='" + receiveServerHost + '\'' +
                ", authCode='" + authCode + '\'' +
                ", sendMailer='" + sendMailer + '\'' +
                ", receiveMailer='" + receiveMailer + '\'' +
                ", sendProtocol=" + sendProtocol +
                ", receiveProtocol=" + receiveProtocol +
                ", sendAlias='" + sendAlias + '\'' +
                ", receiveAlias='" + receiveAlias + '\'' +
                ", enableDebug=" + enableDebug +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MailSource that = (MailSource) o;
        return Objects.equals(sendServerHost, that.sendServerHost) && Objects.equals(authCode, that.authCode) && Objects.equals(sendMailer, that.sendMailer) && sendProtocol == that.sendProtocol;
    }

    @Override
    public int hashCode() {
        return Objects.hash(sendServerHost, authCode, sendMailer, sendProtocol);
    }

    /* **********************************************************************************
     *
     *          链式调用
     *
     * *********************************************************************************
     * */

    public MailSource sendServerHost(String sendServerHost) {
        this.setSendServerHost(sendServerHost);
        return this;
    }

    public MailSource receiveServerHost(String receiveServerHost) {
        this.setReceiveServerHost(receiveServerHost);
        return this;
    }

    public MailSource authCode(String authCode) {
        this.setAuthCode(authCode);
        return this;
    }

    public MailSource sendMailer(String sendMailer) {
        this.setSendMailer(sendMailer);
        return this;
    }

    public MailSource sendProtocol(MailProtocolEnum sendProtocol) {
        this.setSendProtocol(sendProtocol);
        return this;
    }

    public MailSource receiveProtocol(MailProtocolEnum receiveProtocol) {
        this.setReceiveProtocol(receiveProtocol);
        return this;
    }

    public MailSource sendAlias(String sendAlias) {
        this.setSendAlias(sendAlias);
        return this;
    }

    public MailSource ReceiveAlias(String receiveAlias) {
        this.setReceiveAlias(receiveAlias);
        return this;
    }

    public MailSource enableDebug(boolean enableDebug) {
        this.setEnableDebug(enableDebug);
        return this;
    }
}
